const message = {

	async getAll() {
		const {
			data: res,
			status
		} = await uni.$http.get("/msg/getAll?fid=" + uni.$store.state.fridge.default_fridge.fid)
		if (status != 200)
			console.log("还没绑定冰箱");
		if (res.code !== uni.$code.successCode) return uni.$showMsg(res.msg)
		uni.$store.commit('message/updateMessageList', res.data.msgList)
		this.getLatestMessage()
	},

	async getLatestMessage() {
		const {
			data: res
		} = await uni.$http.get("/msg/getLatestMessage?fid=" + uni.$store.state.fridge.default_fridge.fid)
		if (res.code !== uni.$code.successCode) return uni.$showMsg(res.msg)
		uni.$store.commit('message/updateLatestMessage', res.data)
	},

	async setRead(msgIds) {
		const {
			data: res
		} = await uni.$http.post("/msg/setAllRead?msgIds=" + msgIds)
		if (res.code !== uni.$code.successCode) return uni.$showMsg(res.msg)
		this.getAll()
	},

	async handle(msgId) {
		const {
			data: res
		} = await uni.$http.put("/msg/handle/" + msgId)
		if (res.code !== uni.$code.successCode) return uni.$showMsg(res.msg)
		uni.$showMsg(res.msg)
		this.getAll()
	},

	async deleteRead(msgIds) {
		const {
			data: res
		} = await uni.$http.delete("/msg/delAllRead?msgIds=" + msgIds)
		if (res.code !== uni.$code.successCode) return uni.$showMsg(res.msg)
		uni.$showMsg(res.msg)
		this.getAll()
	}
}

export default message